package com.cssiot.webApi.web;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cssiot.webApi.config.PropertySourceConfig;
import com.cssiot.webApi.entity.UserEntity;
import com.cssiot.webApi.mapper.UserSqlServerMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.ehcache.CacheManager;

@RestController
@Api("使用mybatis")
public class UserController {
	private static Logger log = Logger.getLogger(UserController.class);
	@Autowired
	private CacheManager cacheManager;
	@Resource
	private PropertySourceConfig PC;
	@Resource
	private HibernateTemplate hibernateTemplate;
	@Autowired
	private UserSqlServerMapper user1Mapper;

	@ApiOperation(value = "查询所有用户", notes = "整合分页插件pagehelper+Swagger2构建RESTful API文档")
	@GetMapping("/getUsers")
	public List<UserEntity> getUsers() {
		System.out.println(cacheManager.getCache("alarmPileNumTime"));
		System.out.println(hibernateTemplate);
		List<UserEntity> users = user1Mapper.getAll();
		log.debug("jacking--test");
		System.out.println(PC);
		return users;
	}

	@ApiOperation(value = "根据id查询用户", notes = "查询单用户")
	// @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType
	// = "Long")
	@GetMapping("/getUser/{id}")
	public UserEntity getUser(@PathVariable("id") long id) {
		UserEntity user = user1Mapper.getOne(id);
		return user;
	}

	@ApiOperation(value = "新增用户", notes = "新增用户")
	// @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType
	// = "Long")
	@PostMapping("/add")
	public void save(UserEntity user) {
		user1Mapper.insert(user);
	}

	@ApiOperation(value = "根据数据修改用户", notes = "修改用户")
	// @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType
	// = "json")
	@PutMapping(value = "update")
	public void update(UserEntity user) {
		user1Mapper.update(user);
	}

	@ApiOperation(value = "根据id删除用户", notes = "删除用户")
	// @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType
	// = "long")
	@DeleteMapping(value = "/delete/{id}")
	public void delete(@PathVariable("id") long id) {
		user1Mapper.delete(id);
	}

}